// pages/settlement/ticket_info/ticket_info.js
const HOST = require('../../../config.js').host
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeIdx:0,
    addressIdx:'',
    amount:0,
    addressesId:'',
    number:0,
    onOff:false,
    orderId:"",
    userName:"",
    userMobile:"",
    noticeInfo:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    console.log(options)
        console.log
        that.setData({
          orderId:options.orderId,
          typeIdx: options.typeIdex,
          amount: options.amount,
          number: that.sum(options.amount, options.typeIdx), //|| 1  测试数据 2 ====+++++++++++++++++++++====
          userMobile:options.linkmanPhone,
          userName:options.linkman,
          noticeInfo: options.noticeInfo
        })

  },
  // 计算输入框总数
  sum: (amount, typeIdx)=>{
    let number = 0
    if (typeIdx == 0 || typeIdx == 1 || typeIdx == 4 || typeIdx == 5 || typeIdx == 10 || typeIdx == 11){
      number = amount*1
    } else if (typeIdx == 2 || typeIdx == 6){
      number = amount*2
    } else if (typeIdx == 3 || typeIdx == 7){
      number = amount*3
    } else if (typeIdx == 8 || typeIdx == 9){
      number = amount*2 
    }
    console.log(number)
    return number
  }
  ,
  // 提交表单数据
  formSubmit: function (e) {
    let self = this

    console.log(this.data.orderId)
    console.log(this.data.addressesId, e.detail.value)
    console.log(JSON.stringify(e.detail.value))
    if (this.beforeSub(e.detail.value)){

      // return false;//测试终止符====+++++++++++++++++++++====

      wx.request({
        // url: `${HOST}v1/ticketrecord/saveUserCode`,
        url:`${HOST}v1/ticketrecord/saveTicket`,
        method: 'POST',
        data: {
          userId:app.userId,
          userCode: JSON.stringify(e.detail.value),
          identCode:self.data.orderId,
          userMobile:self.data.userMobile,
          userName:self.data.userName,
          recordStatu: 'B'
        },
        header: {
          'content-type': 'application/json'
        },
        success:res=>{
          console.log(res)
          wx.reLaunch({
            url: '../ticket_detail/ticket_detail',
          })
        },
        
      });
      // let customInfoList = JSON.stringify(e.detail.value);
      // let noticeInfo = JSON.parse(this.data.noticeInfo);
      // var userMessage = wx.getStorageSync('userMessage') || "";
      // wx.request({
      //   url: `${HOST}v1/sendordermessage/${self.data.userName}/${self.data.userMobile}/${customInfoList}/${noticeInfo.theDay}/${noticeInfo.ticketType}/${noticeInfo.amount}/${userMessage}`,
      //   success: function (res) {
      //     wx.removeStorageSync('userMessage');
      //   }
      // })
    }
  }
  ,
  // 判断数据结构
  onWrite:(e)=>{
    let that = this
    let str = e.detail.value;
    let onOff = false;
    console.log(str)
    console.log(e.target.dataset.num)
    if(str.replace(/[^0-9]+/g, '').length !== 18){
      wx.showToast({
        title: `第${e.target.dataset.num}个输入框信息有误`,
        image:'../../../assets/images/error.png',
        duration: 2000
      })       
    }
  }
  ,
  // 提交前判断
  beforeSub:p=>{
    let onOff = false
    for(let i in p){
      let name = p[i].replace(/[0-9]+/g, '')
      if (p[i].replace(/[^0-9]+/g, '').length !== 18 || !name) {
        wx.showToast({
          title: `第${Number(i) + Number(1)}个输入框信息有误`,
          image: '../../../assets/images/error.png',
          duration: 2000
        })
        onOff = false
        return  onOff      
      }else{
        onOff = true
      }
    }
    return onOff
  }
  ,

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})